package com.powergisol.gis.util;

import java.io.File;

/**
 * Created By marc
 * Date: 2019/11/1  15:33
 * Description:
 */
public class FileSuffixRename {
    public static void main(String[] args) {
        reName("E:\\osgbJar\\baihuatai_osgb", "lod", "txt");
    }


    /**
     *
     * @param path 输入要修改文件后缀名的文件夹
     * @param from 修改前的后缀名
     * @param to 修改后的后缀名
     */
    public static void reName(String path, String from, String to) {
        File f = new File(path);
        File[] fs = f.listFiles();
        for (File subFile : fs) {
            // 如果文件是文件夹则递归调用批量更改文件后缀名的函数
            if (subFile.isDirectory()) {
                reName(subFile.getPath(), from, to);
            } else {
                String name = subFile.getName();
                if (name.endsWith(from)) {
                    subFile.renameTo(new File(subFile.getParent() + File.separator+ name.substring(0, name.indexOf(from)) + to));
                }
            }
        }
    }
}
